home *** CD-ROM | disk | FTP | other *** search
/ Total Network Tools 2002 / NextStepPublishing-TotalNetworkTools2002-Win95.iso / Archive / Misc Servers / Zope.exe / KILLPCGI.PY < prev    next >
Encoding:
Python Source  |  1998-10-19  |  2.7 KB  |  82 lines

  1. #!/usr/bin/env python
  2. # killpcgi.py - kill a running pcgi process
  3. # Copyright(c) 1998, Jeff Bauer, All rights reserved.
  4.  
  5. # killpcgi is a convenience script to kill a running
  6. # pcgi process, provided that it is running on a 
  7. # Unix system.  You pass it the path of the pcgi info
  8. # file, and killpcgi will kill the running process and 
  9. # remove the socket/pid files.
  10. #
  11. # killpcgi has been built at the request of several users,
  12. # but you may want to first examine the script and modify it 
  13. # to suit your environment.
  14. #
  15. # Bugs:  skimpy error handling
  16.  
  17. # 0.3a  minor win32 exception caught
  18. # 0.2a  Added support for NT - 8/8/98
  19. # 0.1a  Initial version
  20.  
  21. __version__ = "0.3a"
  22.  
  23. def win32_kill(pid):
  24.     """posix-style kill command for the Win32 environment"""
  25.     import win32api, pywintypes
  26.     try:
  27.         handle = win32api.OpenProcess(1, 0, pid)
  28.     except pywintypes.error:
  29.         return -1
  30.     return (0 != win32api.TerminateProcess(handle, 0))
  31.  
  32. def killpcgi(pcgiInfoFile):
  33.     import os, sys, string
  34.     delimiter = '='
  35.     pidFile = None
  36.     socketFile = None
  37.     infoFile = open(pcgiInfoFile, 'r')
  38.     for i in infoFile.readlines():
  39.         if delimiter in i:
  40.             n,v = string.split(string.strip(i), delimiter)
  41.             if n == 'PCGI_PID_FILE':
  42.                 pidFile = v
  43.             elif n == 'PCGI_SOCKET_FILE':
  44.                 socketFile = v
  45.     infoFile.close()
  46.  
  47.     if pidFile is None:
  48.         print "unknown pid file"
  49.     else:
  50.         f = open(pidFile)
  51.         pid = int(f.read())
  52.         f.close()
  53.         if os.name == 'nt':
  54.             if not win32_kill(pid):
  55.                 print "process %d killed" % pid
  56.         elif os.name == 'posix':
  57.             if os.kill(pid, 0):
  58.                 print "process %d doesn't appear to be running" % pid
  59.             else:
  60.                 # safety measure (exclude it if you're fearless)
  61.                 if os.system('/bin/ps -p %d | grep python > /dev/null' % pid):
  62.                     print "process %d doesn't appear to be python" % pid
  63.                 else:
  64.                     os.kill(pid, 15)
  65.                     print "process %d killed" % pid
  66.                     # conservative approach: don't remove pid/socket files
  67.                     # unless we're reasonably certain we have killed the 
  68.                     # running process
  69.                     os.unlink(pidFile)
  70.                     if socketFile is not None: 
  71.                         os.unlink(socketFile)
  72.         else:
  73.             print "kill not supported for platform:", os.name
  74.  
  75. if __name__ == '__main__':
  76.     import sys
  77.     usage = "usage: killpcgi pcgi-info-file"
  78.     if len(sys.argv) < 2:
  79.         print usage
  80.     else:
  81.         killpcgi(sys.argv[1])
  82.